<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="add('0', 1)">添加权限</el-button>
        </template>
      </page-tools>
      <el-card class="box-card">
        <!-- 表格 -->
        <el-table
          border
          :data="list"
          row-key="id"
        >
          <el-table-column align="center" label="名称" prop="name" />
          <el-table-column align="center" label="标识" prop="code" />
          <el-table-column align="center" label="描述" prop="description" />
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-button v-if="scope.row.pid === '0'" type="text" @click="add(scope.row.id, 2, scope.row.code)">添加</el-button>
              <el-button type="text" @click="set(scope.row.id,scope.row.pid, scope.row.code)">编辑</el-button>
              <el-button type="text" @click="del(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-dialog
          :title="type"
          :visible.sync="dialogVisible"
          width="30%"
          center
          :close-on-click-modal="false"
          :destroy-on-close="true"
        >
          <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
            <el-form-item label="权限名称" prop="name">
              <el-input v-model="formData.name" style="width:90%" />
            </el-form-item>
            <el-form-item v-if="formData.type === 2" label="权限标识" prop="code">
              <el-select v-model="formData.code" placeholder="请选择权限标识" style="width:90%">
                <el-option label="用户添加权限" :value="`POINT-USER-ADD-${code}`" />
                <el-option label="用户编辑权限" :value="`POINT-USER-UPDATE-${code}`" />
                <el-option label="用户删除权限" :value="`POINT-USER-DELETE-${code}`" />
              </el-select>
            </el-form-item>
            <el-form-item v-else label="权限标识" prop="code">
              <el-input v-model="formData.code" style="width:90%" />
            </el-form-item>
            <el-form-item label="权限描述">
              <el-input v-model="formData.description" style="width:90%" />
            </el-form-item>
            <el-form-item label="开启">
              <el-switch
                v-model="formData.enVisible"
                active-value="1"
                inactive-value="0"
              />
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
          </span>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { tranListToTreeData } from '@/utils/index'
import { getPermissions, addPermission, delPermission, getPermission, upDatePermission } from '@/api/permission'
import { mapGetters } from 'vuex'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      dialogVisible: false,
      type: '',
      formData: {},
      rules: {
        name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入权限标识', trigger: 'change' }]
      },
      code: ''
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  watch: {
    dialogVisible(now) {
      if (!now) {
        this.formData = {}
      }
    }
  },
  created() {
    this.getPermissions()
  },
  methods: {
    async getPermissions() {
      const result = await getPermissions()
      // console.log(result)
      this.list = tranListToTreeData(result, '0')
    },
    del(id) {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPermission(id)
        this.getPermissions()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    add(pid, type, code) {
      this.formData.pid = pid
      this.formData.type = type
      this.code = code
      this.type = '新增权限'
      this.dialogVisible = true
    },
    async set(id, pid, code) {
      if (pid === '0') {
        this.code = code
      } else {
        // indexOf(查询的字符， 起始位置)
        const index = code.indexOf('-', 13)
        this.code = code.substr(index + 1)
      }
      this.pid = id
      this.formData = await getPermission(id)
      this.type = '编辑权限'
      this.dialogVisible = true
    },
    submitForm() {
      this.$refs.perForm.validate(async(valid) => {
        if (valid) {
          if (this.type === '新增权限') {
            if (this.userInfo.roles.points.includes(this.formData.code)) {
              this.$message.info('该标识已添加')
              this.dialogVisible = false
              return false
            }
            const { code } = await addPermission(this.formData)
            this.userInfo.roles.points.push(code)
            console.log(this.userInfo)
            this.$message.success('添加成功')
          } else if (this.type === '编辑权限') {
            await upDatePermission({ ...this.formData, id: this.pid })
            this.$message.success('编辑成功成功')
          }
          this.getPermissions()
          this.dialogVisible = false
        } else {
          this.$message.error('操作失败')
        }
      })
    }
  }
}
</script>
